package com.sskj.mine.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;
import android.widget.Button;

import com.alibaba.android.arouter.launcher.ARouter;
import com.allen.library.SuperTextView;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.LanguageUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.AppVersionBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.lib.ui.fragment.AppUpDateFragment;
import com.sskj.lib.util.APKVersionCodeUtils;
import com.sskj.lib.util.AppUtil;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.util.TipUtil;
import com.sskj.mine.BuildConfig;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.SettingPresenter;

import java.util.Locale;

import javax.inject.Inject;

import butterknife.BindView;
import carlwu.top.skin_core.SkinManager;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import sskj.lee.appupdatelibrary.BaseUpdateDialogFragment;
import sskj.lee.appupdatelibrary.BaseVersion;

/**
 * @author Hey
 * Create at  2019/05/03
 */
public class SettingActivity extends BaseActivity<SettingPresenter> {

    @BindView(R2.id.menu_language)
    SuperTextView menuLanguage;
    @BindView(R2.id.menu_version)
    SuperTextView menuVersion;
    @BindView(R2.id.menu_theme)
    SuperTextView menuTheme;
    @BindView(R2.id.logout_btn)
    Button logout_btn;


    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private BottomSheetDialog bottomSheet;
    private Disposable languageDispo;
    private BottomSheetDialog bottomSheetTheme;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_setting;
    }

    @Override
    public SettingPresenter getPresenter() {
        return new SettingPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
                logout_btn.setVisibility(View.GONE);
            }
        });
        setTitle("设置");
        menuLanguage.setRightString(CommonUtil.dealLanguage(LanguageUtil.getAppLocale(App.INSTANCE).toString()));

    }


    @Override
    protected void initEvent() {
        ClickUtil.click(menuLanguage, this::selectLanguage);

        ClickUtil.click(logout_btn, () -> {
            TipUtil.getSureTip(this, "退出登录", "是否确定退出该账户？", "退出", () -> {

                LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
                logoutProvider.logout();
            });
        });
        ClickUtil.click(menuTheme, () -> {
            bottomSheetTheme = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, position, view) -> {
                bottomSheetTheme.dismiss();
                if (position == 0) {
                    SkinManager.getInstance().loadSkin(null);
                } else {
                    SkinManager.getInstance().loadSkin("/sdcard/app-skin-debug.skin");
                }

            }, "白", "黑");
            bottomSheetTheme.show();
        });


    }


    private void selectLanguage() {
        languageDispo = Flowable.fromIterable(LanguageUtil.locales)
                .map(locale -> CommonUtil.dealLanguage(locale.toString()))
                .toList()
                .subscribe((strings, throwable) -> {
                    bottomSheet = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, position, view) -> {
                        bottomSheet.dismiss();
                        changeLanguage(LanguageUtil.getAppLocaleByToString(CommonUtil.rdealLanguage(strings.get(position))));
                    }, strings);
                    bottomSheet.show();
                });
    }

    private void changeLanguage(Locale locale) {

        LanguageUtil.saveLanguageSetting(App.INSTANCE, locale);

        AppUtil.changeLanguage();

    }


    @Override
    public void initData() {
        String versionName = BuildConfig.VERSION_NAME;
        menuVersion.setRightString("V" + versionName);
        ClickUtil.click(menuVersion, () -> {
            mPresenter.checkVersion();
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    public void updateUi() {
        userViewModel.update();
    }


    public void onVersionDataSuccess(AppVersionBean versionBean) {
        if (versionBean == null) {
            return;
        }
        String verName = APKVersionCodeUtils.getVerName(this);
        versionBean.setViewStyle(BaseVersion.NOTIFYCATION_STYLE);

        if (APKVersionCodeUtils.compareVersion(versionBean.getAppVersion(), verName) == 1) {
            android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            getFragmentManager().beginTransaction();
            AppUpDateFragment appUpDateFragment = new AppUpDateFragment();//自定义UI
            Bundle bundle = new Bundle();
            bundle.putSerializable(BaseUpdateDialogFragment.INTENT_KEY, versionBean);
            appUpDateFragment.setArguments(bundle);
            appUpDateFragment.show(fragmentTransaction, "update");
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        userViewModel.update();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DisposUtil.close(languageDispo);
    }


}
